<template>
	<view>
		<view class="box">
			<image :src="isTrue?'/static/images/ic_tongguo.png': '/static/images/ic_jujue.png'"></image>
			<text class="box_font">{{ isTrue ? '核验通过' : '核验拒绝' }}</text>
			<!-- <text v-if="!isTrue" class="box_fontb">客户购买的凭证已失效，不能进场</text> -->
			<view v-if="isLoing && info" class="box_ctn">
				<view class="xm">核验结果</view>
				<view v-if="info.objName">活动名称：{{info.objName}}</view>
				<view v-if="info.handleName">报名人：{{info.handleName}}</view>
				<view v-if="info.reserveTime">预定时间：{{info.reserveTime}}</view>
				<view v-if="info.memberName">会员：{{info.memberName}}</view>
				<view v-if="info.err">{{info.err}}</view>
			</view>
			<view class="btns">
				<view @click="jump3('/pages/index/index')">回到首页</view>
				<view @click="getCode">继续核验</view>
			</view>
		</view>
	</view>
</template>

<script>
	let Qrcode = require('@/utils/reqrcode.js')
	// #ifdef H5
	import wx from '@/common/jweixin.js';
	// #endif
	export default {
		data() {
			return {
				isTrue: true,
				isLoing: false,
				info: null
			};
		},
		onLoad(options) {
			this.getDetail(options.code);
			this.getSignature();
		},
		methods: {
			jump3(url, params, a) {
				this.$app.jump3(url, params, a);
			},
			getCode() {
				let that = this
				// #ifdef MP
				uni.scanCode({
					success(res) {
						console.log(2321, res)
						that.getDetail(res.result)
					}
				});
				// #endif
				// #ifdef H5
				this.onScan(1)
				// #endif
			},
			getSignature() {
				var url = location.href.split('#')[0];
				this.$app.ajax({
					data: {
						param: {
							path: url
						}
					},
					api: this.$api.wxpConfig()
				}).then((res) => {
					if (res.errorCode === '000000') {
						this.setShare(res.record)
					}
				}).catch(() => {})
			},
			// 设置分享数据
			setShare(signature) {
				let that = this
				var url = location.href.split('?')[0]
				uni.setStorageSync('signature', JSON.stringify(signature))

				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					// 配置微信 JSSDK
					appId: signature.appId, // 必填，公众号的唯一标识
					timestamp: signature.timestamp, // 必填，生成签名的时间戳
					nonceStr: signature.nonceStr, // 必填，生成签名的随机串
					signature: signature.signature, // 必填，签名
					jsApiList: ['scanQRCode', 'checkJsApi',
						'chooseImage'
					], // 必填，需使用的JS接口列表
				})

				wx.ready(function() {
					console.log('这是分享功能')

				})
				wx.error((res) => {
					console.log(res)
					// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
					// that.getSignature()
				})
			},
			isWeiXin() {
				// #ifndef H5
				return false;
				// #endif
				var ua = navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == "micromessenger") {
					console.log('微信公众号环境');
					return true;
				} else {
					return false;
				}
			},
			// h5扫描二维码并解析
			onScan(type) {
				let that = this
				wx.scanQRCode({
					needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
					scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
					success: function(res) {
						var result = res.resultStr; // 当 needResult 为 1 时，扫码返回的结果
						var resultArr = result.split(','); // 扫描结果以逗号分割数组(一维码)
						var codeContent = resultArr[resultArr.length - 1]; // 获取数组最后一个元素，也就是最终的内容 
						that.getDetail(codeContent)
					},
					fail: function(response) {
						console.log("调用wx.scanQRCode扫码失败");
					},
				});
				// uni.chooseImage({
				// 	count: 1,
				// 	success: imgRes => {
				// 		Qrcode.qrcode.decode(this.getObjectURL(imgRes.tempFiles[0]))
				// 		Qrcode.qrcode.callback = (codeRes) => {
				// 			if (codeRes.indexOf('error') >= 0) {
				// 				// 二维码识别失败
				// 				console.log('不合法二维码：', codeRes)
				// 			} else {
				// 				// 二维码识别成功
				// 				let r = that.decodeStr(codeRes)
				// 				console.log(2222, r)
				// 				if(type == 1) {
				// 					that.getDetail(r)
				// 				}
				// 			}
				// 		}
				// 	}
				// })
			},
			// 解码，输出：中文
			decodeStr(str) {
				var out, i, len, c;
				var char2, char3;
				out = "";
				len = str.length;
				i = 0;
				while (i < len) {
					c = str.charCodeAt(i++);
					switch (c >> 4) {
						case 0:
						case 1:
						case 2:
						case 3:
						case 4:
						case 5:
						case 6:
						case 7:
							// 0xxxxxxx
							out += str.charAt(i - 1);
							break;
						case 12:
						case 13:
							// 110x xxxx 10xx xxxx
							char2 = str.charCodeAt(i++);
							out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
							break;
						case 14:
							// 1110 xxxx 10xx xxxx 10xx xxxx
							char2 = str.charCodeAt(i++);
							char3 = str.charCodeAt(i++);
							out += String.fromCharCode(((c & 0x0F) << 12) |
								((char2 & 0x3F) << 6) |
								((char3 & 0x3F) << 0));
							break;
					}
				}
				return out;
			},
			// 获取文件地址函数
			getObjectURL(file) {
				var url = null
				if (window.createObjectURL !== undefined) { // basic
					url = window.createObjectURL(file)
				} else if (window.URL !== undefined) { // mozilla(firefox)
					url = window.URL.createObjectURL(file)
				} else if (window.webkitURL !== undefined) { // webkit or chrome
					url = window.webkitURL.createObjectURL(file)
				}
				return url
			},
			getDetail(code) {
				this.isLoing = false
				this.$app.ajax({
					data: {
						param: {
							venueId: uni.getStorageSync('GYMID'),
							code
						}
					},
					api: this.$api.activityVerification()
				}).then(res => {
					this.isLoing = true
					if (res.errorCode != '000000') {
						this.isTrue = false
						this.info = {
							err: res.errorMsg
						}
					} else {
						this.isTrue = true
						this.info = res.record
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.box {
		display: flex;
		align-items: center;
		flex-direction: column;
		margin-top: 120rpx;
	}

	.box image {
		width: 200rpx;
		height: 220rpx;
	}

	.box_font {
		font-size: 34rpx;
		font-weight: 600;
		color: #222222;
		margin-top: 20rpx;
	}

	.box_fontb {
		color: #333333;
		font-size: 28rpx;
		margin-top: 20rpx;
	}

	.box_ctn {
		width: 690rpx;
		padding: 30rpx 40rpx;
		box-sizing: border-box;
		margin-top: 70rpx;
		color: #333333;
		font-size: 28rpx;
		line-height: 1.5;
		background: #f7f7f7;
		border-radius: 16rpx;

		view {
			margin-top: 18rpx;
		}
	}

	.box_ctn .xm {
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: bolder;
		color: #222222;
		margin-top: 0;
	}

	.btns {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 430rpx;
		margin: 80rpx auto 0;

		view {
			width: 200rpx;
			text-align: center;
			border-radius: 36rpx;
			border: 1rpx solid rgba(153, 153, 153, 0.5);
			font-size: 28rpx;
			color: #555555;
			line-height: 70rpx;

			&:last-of-type {
				color: $color;
				border-color: $color;
			}
		}
	}
</style>